using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Silverlight.Testing;

public class UITestHelper : IDisposable
{
    private readonly SilverlightTest testClass;
    private readonly FrameworkElement elementUnderTest;
    private readonly Canvas testSurface= new Canvas();

    public UITestHelper(SilverlightTest SilverlightTest, FrameworkElement frameworkElement)
    {
        testClass = SilverlightTest;
        this.elementUnderTest = frameworkElement;
        LoadUI();
    }

    public  EnqueuedSleep WaitFor(int timeIncrementInSeconds)
    {
        return new EnqueuedSleep(testClass, timeIncrementInSeconds);
    }

    public void LoadUI()
    {
        DelegateHandler e = new DelegateHandler();
        elementUnderTest.Loaded += e.GenericDelegateHandler;
        testSurface.Children.Add(elementUnderTest);
        testClass.Silverlight.TestSurface.Children.Add(testSurface);
        testClass.EnqueueConditional(e.DelegateHasBeenInvoked);
    }

    public void Dispose()
    {
        testClass.EnqueueCallback(() => testClass.Silverlight.TestSurface.Children.Remove(testSurface));
        testClass.EnqueueTestComplete();
    }

    public void Exec(GenericDelegate genericDelegate)
    {
        testClass.EnqueueCallback(genericDelegate);
    }

    public EnqueuedCondition WaitUntil(ConditionDelegate conditionDelegate)
    {
        return new EnqueuedCondition(conditionDelegate,testClass);
    }
}